home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / system / bf_171a.zip / SWITCH.C < prev   
C/C++ Source or Header  |  1991-01-07  |  9KB  |  237 lines

  1. /*****************************************************************************/
  2. /* This program's purpose is to allow switching between Back & Forth tasks   */
  3. /* from the command line. The source code is provided for other programs to  */
  4. /* build direct hooks into B&F.                                              */
  5. /*                                                                           */
  6. /* This code is written in Turbo-C and is compatible with Microsoft-C and    */
  7. /* Watcom-C.                                                                 */
  8. /*****************************************************************************/
  9.  
  10. /*****************************************************************************/
  11. /* Needed header files...                                                    */
  12. /*****************************************************************************/
  13. #include "ctype.h"
  14. #include "dos.h"
  15. #include "stdio.h"
  16. #include "stdlib.h"
  17. #include "string.h"
  18.  
  19. /*****************************************************************************/
  20. /* Back & Forth Kernel calls are initiated through interrupt 12h by setting  */
  21. /* AX & CX to -2 and placing the function call in BX.                        */
  22. /*                                                                           */
  23. /* Current function calls:                                                   */
  24. /*                                                                           */
  25. /* BX = 0 -- B&F kernel status. Returns 1 if B&F is in memory.               */
  26. /* BX = 2 -- Returns the # of defined programs and an array of defined       */
  27. /*           program IDS. The array to place the defined program ids in      */
  28. /*           should be passed in ES:DI. The # of programs defined will be    */
  29. /*           returned in AX.                                                 */
  30. /* BX = 3 -- Switch tasks! Place the program id in DX. If the specified      */
  31. /*           program id is undefined, AX will be 0 else non-zero!            */
  32. /*****************************************************************************/
  33.  
  34. /*****************************************************************************/
  35. /* Local function prototypes...                                              */
  36. /*****************************************************************************/
  37. void build_program_id_list(void);
  38. void check_if_bf_installed(void);
  39. void display_program_id_list(void);
  40. void display_switch_help(void);
  41. void switch_task(int program_id);
  42.  
  43. /*****************************************************************************/
  44. /* Local variables...                                                        */
  45. /*****************************************************************************/
  46. int no_of_defined_programs;      /* Current # of defined programs.           */
  47. int program_ids[50];             /* Back & Forth currently has a limit of 50 */
  48.                                  /* program definitions.                     */
  49.  
  50. /*****************************************************************************/
  51. /* Switch starts here.                                                       */
  52. /*                                                                           */
  53. /* Syntax is:  switch <cc> or switch list                                    */
  54. /*                                                                           */
  55. /*             where <cc> is a the two character id of the task to switch    */
  56. /*             to. If the word "list" is on the command line, a list of      */
  57. /*             program ids will be listed. To bring up the Back & Forth      */
  58. /*             menu, run switch with the word "menu".                        */
  59. /*****************************************************************************/
  60. void cdecl main(int argc,char *argv[])
  61. {
  62.   /* Make sure B&F is in memory */
  63.   check_if_bf_installed();
  64.  
  65.   /* Make sure 2 arguments exactly */
  66.   if (argc != 2)
  67.   {
  68.     display_switch_help();
  69.   }
  70.  
  71.   /* Switch to menu ?? */
  72.   if (memicmp(argv[1],"menu",4) == 0)
  73.   {
  74.     switch_task(0);
  75.   }
  76.  
  77.   /* Display program id list ?? */
  78.   if (memicmp(argv[1],"list",4) == 0)
  79.   {
  80.     /* Build program id list */
  81.     build_program_id_list();
  82.  
  83.     display_program_id_list();
  84.   }
  85.  
  86.   /* Switch task ?? Check for two letter id */
  87.   if (isalnum(*argv[1]) && isalnum(*(argv[1]+1)) && *(argv[1]+2) == '\0')
  88.   {
  89.     /* Convert characters to uppercase */
  90.     strupr(argv[1]);
  91.  
  92.     /* Reverse bytes because of stupid 8086/8088 architecture! */
  93.  
  94.     switch_task(*(int *) argv[1]);
  95.   }
  96.  
  97.   /* Inform user of invalid argument and display instructions */
  98.   printf("Invalid option '%s'!",argv[1]);
  99.   display_switch_help();
  100. }
  101.  
  102. /*****************************************************************************/
  103. /* build_program_id_list()                                                   */
  104. /*                                                                           */
  105. /* Gets the list of program ids currently defined in Back & Forth! The id    */
  106. /* list is returned in supplied integer array of 50. The address of the      */
  107. /* array should be passed in ES:DI.                                          */
  108. /*****************************************************************************/
  109. void build_program_id_list(void)
  110. {
  111.   union  REGS  regs;
  112.   struct SREGS sregs;
  113.  
  114.   /* Call B&F kernel */
  115.   regs.x.ax = -2;
  116.   regs.x.cx = -2;
  117.   regs.x.bx =  2;
  118.   regs.x.di = (unsigned) program_ids;
  119.   sregs.es  = FP_SEG(((void far *) program_ids));
  120.   int86x(0x12,®s,®s,&sregs);
  121.  
  122.   /* # of programs defined is returned in AX */
  123.   no_of_defined_programs = regs.x.ax;
  124. }
  125.  
  126. /*****************************************************************************/
  127. /* check_if_bf_installed()                                                   */
  128. /*                                                                           */
  129. /* Makes sure Back & Forth is in memory. If not, exits to DOS with error msg.*/
  130. /*****************************************************************************/
  131. void check_if_bf_installed(void)
  132. {
  133.   union REGS regs;
  134.  
  135.   /* B&F memory resident ?? */
  136.   regs.x.ax = -2;
  137.   regs.x.cx = -2;
  138.   regs.x.bx =  0;
  139.   int86(0x12,®s,®s);
  140.  
  141.   /* AX == 1 if B&F is resident */
  142.   if (regs.x.ax != 1)
  143.   {
  144.     puts("Back & Forth is not loaded!");
  145.     exit(0);
  146.   }
  147. }
  148.  
  149. /*****************************************************************************/
  150. /* display_program_id_list()                                                 */
  151. /*                                                                           */
  152. /* Displays currently defined program ids!                                   */
  153. /*****************************************************************************/
  154. void display_program_id_list(void)
  155. {
  156.   char  high_letter;
  157.   int   id_no;
  158.   char *id_ptr;
  159.   char  low_letter;
  160.  
  161.   /* Add a blank line */
  162.   puts("");
  163.  
  164.   /* Loop through list */
  165.   id_ptr = (char *) program_ids;
  166.   for (id_no = 1; id_no <= no_of_defined_programs; ++id_no)
  167.   {
  168.     /* Id defined ?? */
  169.     if (*id_ptr != 0)
  170.     {
  171.       low_letter   = *id_ptr++;
  172.       high_letter  = *id_ptr++;
  173.       printf("[%c%c] ",low_letter,high_letter);
  174.     }
  175.     else
  176.     {
  177.       printf("[??] ");
  178.       ++id_ptr;
  179.       ++id_ptr;
  180.     }
  181.  
  182.     /* Start a new line after every 10 definitions */
  183.     if ((id_no % 10) == 0)
  184.     {
  185.       puts("");
  186.     }
  187.   }
  188.  
  189.   /* Return to DOS */
  190.   exit(0);
  191. }
  192.  
  193. /*****************************************************************************/
  194. /* display_switch_help()                                                     */
  195. /*                                                                           */
  196. /* Displays help for switch program.                                         */
  197. /*****************************************************************************/
  198. void display_switch_help(void)
  199. {
  200.   /* Display cumbersome text */
  201.   puts("Switch Version 1.20 Copyright 1990 by Progressive Solutions, Inc\n");
  202.   puts("Syntax is:  switch <cc> or switch <list> or switch <menu>\n");
  203.   puts("where <c